package com.ninetaildemonfox.zdl.socialproject.mvp.ui.adapter.viewholder;

import android.graphics.drawable.Drawable;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hyphenate.chat.EMClient;
import com.hyphenate.easeui.utils.Preferences;
import com.hyphenate.easeui.utils.map.JSONUtils;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.kongzue.dialog.interfaces.OnDialogButtonClickListener;
import com.kongzue.dialog.util.BaseDialog;
import com.kongzue.dialog.v3.MessageDialog;
import com.ninetaildemonfox.zdl.ninetaildemonfox.utils.AppManager;
import com.ninetaildemonfox.zdl.socialproject.R;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.DateDetailsActivity;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.FeedBackActivity;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.LoginActivity;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.MainActivity;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.PicturePreviewActivity;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.adapter.ImagViewAdapter;
import com.ninetaildemonfox.zdl.socialproject.utils.GlideUtils;
import com.ninetaildemonfox.zdl.socialproject.utils.RoundImageView;
import com.ninetaildemonfox.zdl.socialproject.utils.ToastUtil;
import com.ninetaildemonfox.zdl.socialproject.utils.dialog.CommentActivity;
import com.ninetaildemonfox.zdl.socialproject.utils.dialog.DialogShow;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 创建人： Nine tails fox
 * 创建时间： 2019/7/22
 * 功能描述： 电台界面 的逻辑
 * 联系方式：1037438704@qq.com
 *
 * @author NineTailDemonFox
 */

public class RadioStationViewHolder extends BaseViewHolder<Map<String, String>> {
    @BindView(R.id.text_my_registration)
    TextView textMyRegistration;
    @BindView(R.id.image_head)
    RoundImageView imageHead;
    @BindView(R.id.ll_whole_onclick)
    LinearLayout llWholeOnclick;
    @BindView(R.id.text_comment)
    TextView textComment;
    @BindView(R.id.text_name)
    TextView textName;
    @BindView(R.id.text_create_time)
    TextView textCreateTime;
    @BindView(R.id.text_identification)
    TextView textIdentification;
    @BindView(R.id.text_appintment_time)
    TextView textAppintmentTime;
    @BindView(R.id.text_city)
    TextView textCity;
    @BindView(R.id.text_expect_ids)
    TextView textExpectIds;
    @BindView(R.id.text_remarks)
    TextView textRemarks;
    @BindView(R.id.text_comment_count)
    TextView textCommentCount;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.image_one)
    ImageView image_one;
    @BindView(R.id.image_beian)
    ImageView image_beian;
    @BindView(R.id.ll_dianzan_gone)
    LinearLayout ll_dianzan_gone;
    @BindView(R.id.text_jubao)
    TextView text_jubao;
    private int jubaoInt = 0;
    private int holderType = 1;


    public void setJubaoInt(int jubaoInt) {
        this.jubaoInt = jubaoInt;
    }

    public RadioStationViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
    }

    @Override
    public void setData(final Map<String, String> data) {
        super.setData(data);


        ArrayList<Map<String, String>> list1 = JSONUtils.parseKeyAndValueToMapList(data.get("pics"));
        //        是否是官方的  1是 2否
        String guanfang = data.get("guanfang");
        if (guanfang != null) {
            if (guanfang.equals("1")) {
                image_one.setVisibility(View.VISIBLE);
                recyclerView.setVisibility(View.GONE);
                textMyRegistration.setVisibility(View.GONE);
                text_jubao.setVisibility(View.GONE);
                ll_dianzan_gone.setVisibility(View.GONE);
                textIdentification.setText("官方");
                textIdentification.setTextColor(getContext().getResources().getColor(R.color.white));
                textIdentification.setBackgroundResource(R.drawable.shape_drawable_my_text_bg);
                image_beian.setVisibility(View.GONE);
                textRemarks.setText(data.get("remarks"));
                if (list1.size() == 0) {
                } else {
                    Glide.with(getContext()).load(list1.get(0).get("pic")).into(image_one);
                }
            } else {
                //举报显示隐藏
                jubao();
                textRemarks.setText("备注说明 :" + data.get("remarks"));
                image_beian.setVisibility(View.VISIBLE);
                //        性别判断
                setSex(data);
                //非官方
                ll_dianzan_gone.setVisibility(View.VISIBLE);
                image_one.setVisibility(View.GONE);
                recyclerView.setVisibility(View.VISIBLE);
                //        "type": "2",  // 约会类型 1动态 2报名约会
                if (data.get("type").equals("1")) {
                    textMyRegistration.setVisibility(View.GONE);
                } else {
                    textMyRegistration.setVisibility(View.VISIBLE);
                }
            }
        } else {
            textRemarks.setText("备注说明 :" + data.get("remarks"));
            //        性别判断
            setSex(data);
            //举报显示隐藏
            jubao();
        }


        //头像
        GlideUtils.originalGraph(getContext(), data.get("head_pic"), imageHead);
        textName.setText(data.get("nickname"));
        String identification = data.get("identification");
        // 约会日期
        String create_time = data.get("create_time");
        textCreateTime.setText("发布于" + create_time);
        //创建时间
        if (TextUtils.isEmpty(data.get("appointment_time"))) {
            textAppintmentTime.setVisibility(View.GONE);
        } else {
            textAppintmentTime.setVisibility(View.VISIBLE);
            textAppintmentTime.setText("约会时间：" + data.get("appointment_time") + data.get("appointment_am"));
        }
        //地址
        if (TextUtils.isEmpty(data.get("city"))) {
            textCity.setVisibility(View.GONE);
        } else {
            textCity.setVisibility(View.VISIBLE);
            textCity.setText("约会地点 ：" + data.get("city"));
        }
        //期望对象
        if (TextUtils.isEmpty(data.get("expect_ids"))) {
            textExpectIds.setVisibility(View.GONE);
        } else {
            textExpectIds.setVisibility(View.VISIBLE);
            textExpectIds.setText("期望对象：" + data.get("expect_ids"));
        }


        String baoming_count = "我要报名（" + data.get("baoming_count") + "）";
//  状态  1正常  2结束
        String status = data.get("status");
        Drawable drawable3 = null;
// 是否是自己的动态或约会  1是 2否
        String myself = data.get("myself");
        if (myself != null) {
            if (myself.equals("1")) {
                if (status.equals("2")) {
                    baoming_count = "已结束（" + data.get("baoming_count") + "）";
                    drawable3 = getContext().getResources().getDrawable(R.mipmap.ic_list_sign_no);
                } else {
                    baoming_count = "结束报名（" + data.get("baoming_count") + "）";
                    drawable3 = getContext().getResources().getDrawable(R.mipmap.ic_list_sign_end);
                }
                drawable3.setBounds(0, 0, drawable3.getMinimumWidth(), drawable3.getMinimumHeight());
                textMyRegistration.setText(baoming_count);
            } else if (myself.equals("2")) {
//                "status": "1",     //  1正常  2 已结束 3  不可见
                if (status.equals("1")) {
                    // 是否报名  1是 2否
                    if (data.get("baoming") != null) {
                        if (data.get("baoming").equals("1")) {
                            baoming_count = "已结束（" + data.get("baoming_count") + "）";
                            drawable3 = getContext().getResources().getDrawable(R.mipmap.ic_list_sign_no);
//                            drawable3 = getContext().getResources().getDrawable(R.mipmap.ic_list_sign_up);
                            drawable3.setBounds(0, 0, drawable3.getMinimumWidth(), drawable3.getMinimumHeight());
                            baoming_count = "已报名（" + data.get("baoming_count") + "）";
                            textMyRegistration.setText(baoming_count);
                        } else {
                            drawable3 = getContext().getResources().getDrawable(R.mipmap.ic_list_sign_up);
                            drawable3.setBounds(0, 0, drawable3.getMinimumWidth(), drawable3.getMinimumHeight());
                            textMyRegistration.setText("我要报名（" + data.get("baoming_count") + "）");
                        }
                    } else {
                        drawable3 = getContext().getResources().getDrawable(R.mipmap.ic_list_sign_up);
                        drawable3.setBounds(0, 0, drawable3.getMinimumWidth(), drawable3.getMinimumHeight());
                        textMyRegistration.setText("我要报名（" + data.get("baoming_count") + "）");
                    }

                }
            }
        } else {
//            1正常
            if (status.equals("1")) {
                // 是否报名  1是 2否
                //                "status": "1",     //  1正常  2 已结束 3  不可见
                if (status.equals("1")) {
                    // 是否报名  1是 2否
                    if (data.get("baoming") != null) {
                        if (data.get("baoming").equals("1")) {
                            drawable3 = getContext().getResources().getDrawable(R.mipmap.ic_list_sign_no);
                            drawable3.setBounds(0, 0, drawable3.getMinimumWidth(), drawable3.getMinimumHeight());
                            baoming_count = "已结束（" + data.get("baoming_count") + "）";
                            textMyRegistration.setText(baoming_count);
                        }
                    } else {
                        drawable3 = getContext().getResources().getDrawable(R.mipmap.ic_list_sign_up);
                        drawable3.setBounds(0, 0, drawable3.getMinimumWidth(), drawable3.getMinimumHeight());
                        textMyRegistration.setText("已结束（" + data.get("baoming_count") + "）");
                    }
                } else {
                    drawable3 = getContext().getResources().getDrawable(R.mipmap.ic_list_sign_up);
                    drawable3.setBounds(0, 0, drawable3.getMinimumWidth(), drawable3.getMinimumHeight());
                    textMyRegistration.setText("已结束（" + data.get("baoming_count") + "）");
                }
            }
        }

        if (drawable3 != null) {
            textMyRegistration.setCompoundDrawables(null, drawable3, null, null);
        }

        Drawable drawable = null;
        if (data.get("good_count").equals("0") || TextUtils.isEmpty(data.get("good_count"))) {
            drawable = getContext().getResources().getDrawable(R.mipmap.ic_give_the_thumbs_up_false);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        } else {
            if (data.get("good").equals("2")) {
                drawable = getContext().getResources().getDrawable(R.mipmap.ic_give_the_thumbs_up_true);
            } else {
                drawable = getContext().getResources().getDrawable(R.mipmap.ic_give_the_thumbs_up_false);
            }
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        }
        textCommentCount.setText(data.get("good_count"));
        textCommentCount.setCompoundDrawables(null, drawable, null, null);


        Drawable drawable2;
        //1可以评论
        if (data.get("can_comment").equals("1")) {
            drawable2 = getContext().getResources().getDrawable(R.mipmap.ic_comment);
            drawable2.setBounds(0, 0, drawable2.getMinimumWidth(), drawable2.getMinimumHeight());
        } else {
            drawable2 = getContext().getResources().getDrawable(R.mipmap.ic_comment_no);
            drawable2.setBounds(0, 0, drawable2.getMinimumWidth(), drawable2.getMinimumHeight());
        }

        textComment.setCompoundDrawables(null, drawable2, null, null);


        //相冊
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 4));

        if (list1 == null || list1.size() == 0) {
            recyclerView.setVisibility(View.GONE);
        } else {
            if (guanfang != null) {
                if (guanfang.equals("1")) {
                    recyclerView.setVisibility(View.GONE);
                } else {
                    recyclerView.setVisibility(View.VISIBLE);
                }
            } else {
                recyclerView.setVisibility(View.VISIBLE);
            }
            final ImagViewAdapter imagViewAdapter = new ImagViewAdapter(R.layout.imageview);
            recyclerView.setAdapter(imagViewAdapter);
            imagViewAdapter.setNewData(list1);
            imagViewAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    List<Map<String, String>> data = imagViewAdapter.getData();
                    PicturePreviewActivity.start(getContext(), data, position, 2, "");
                }
            });
        }
        imageHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (guanfang != null) {
                    if (guanfang.equals("1")) {
                        return;
                    }
                }
                if (mOnClick != null) {
                    mOnClick.onClick(getAdapterPosition(), data, 3);
                }
            }
        });

        //我要报名
        textMyRegistration.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity.thisMainCount = 0;
                if (status.equals("2")) {
                    ToastUtil.show("报名已结束");
                    return;
                }
                if (data.get("myself").equals("1")) {
                    if (mOnClick != null) {
                        //  状态  1正常  2结束
                        if (status.equals("2")) {
                            return;
                        }
                        //结束报名
                        mOnClick.onClick(getAdapterPosition(), data, 2);
                    }
                } else {
                    if (data.get("baoming") != null) {
                        // 是否报名  1是 2否
                        if (data.get("baoming").equals("1")) {

                        } else {
                            AppCompatActivity me = (AppCompatActivity) getContext();
                            DialogShow.start(me, "6", data.get("id"), getAdapterPosition(), 10000);
                        }
                    }
                }

            }
        });
        llWholeOnclick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holderType == 1) {
                    // 约会类型 1动态详情 2发布详情
                    DateDetailsActivity.start(getContext(), "1", data.get("id"), data.get("type"));
                }
            }
        });
        textComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 能否评论 1是 2否
                if (data.get("can_comment").equals("1")) {
                    CommentActivity.start(getContext(), data.get("id"));
                } else {
                    ToastUtil.show("禁止评论");
                }
            }
        });
        //点赞
        textCommentCount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnClick != null) {
                    mOnClick.onClick(getAdapterPosition(), data, 1);
                }
            }
        });

        //举报
        text_jubao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FeedBackActivity.start(getContext(), FeedBackActivity.class, "1");
            }
        });
    }

    private void jubao() {
        Log.d("jubaoInt", "===================" + jubaoInt);
        if (jubaoInt == 0) {
            text_jubao.setVisibility(View.VISIBLE);
        } else {
            text_jubao.setVisibility(View.GONE);
        }
    }

    private ItemOnClick mOnClick = null;

    public void setSex(Map<String, String> data) {
        String identification = data.get("identification");
        if (data.get("sex").equals("1")) {
            if (data.get("vip").equals("1")) {
                textIdentification.setText("非会员");
                textIdentification.setTextColor(getContext().getResources().getColor(R.color.white));
                textIdentification.setBackgroundResource(R.drawable.shape_drawable_my_text_bg_false);
            } else {
                textIdentification.setText("会员");
                textIdentification.setTextColor(getContext().getResources().getColor(R.color.white));
                textIdentification.setBackgroundResource(R.drawable.shape_drawable_my_text_bg);
            }

        } else {
            if (identification.equals("1")) {
                textIdentification.setText("未认证");
                textIdentification.setTextColor(getContext().getResources().getColor(R.color.text_black));
                textIdentification.setBackgroundResource(R.drawable.shape_drawable_my_text_bg_false);
            } else {
                textIdentification.setText("已认证");
                textIdentification.setTextColor(getContext().getResources().getColor(R.color.white));
                textIdentification.setBackgroundResource(R.drawable.shape_drawable_my_text_bg);
            }
        }
    }

    //点击事件的转嫁接口
    public interface ItemOnClick {
        void onClick(int position, Map<String, String> data, int type);
    }

    public void setmOnClick(ItemOnClick mOnClick) {
        this.mOnClick = mOnClick;
    }

    public void getType(int holderType) {
        this.holderType = holderType;
    }

}